package com.woniu.smart.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.smart.dto.EnterpricesDTO;
import com.woniu.smart.entity.Enterprise;
import com.woniu.smart.service.EnterpriseService;
import com.woniu.smart.util.Result;
import com.woniu.smart.vo.EChartsEnterpriseVO;
import com.woniu.smart.vo.EnterpriseVo;
import com.woniu.smart.vo.SelByNameEnterpriseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: smart
 * @description: 对租户企业表的操作
 * @author: meibing
 * @create: 2022-10-21 15:12
 **/
@RestController
@RequestMapping("enterprise")
public class EnterpriseController {

    @Autowired
    private EnterpriseService enterpriseService;

    /**
     * 查询全部租户企业表
     *
     * @return list集合
     */
    @PostMapping("queryAll")
    public Result queryAll(@RequestBody EnterpricesDTO enterpricesDTO) {
        PageHelper.startPage(enterpricesDTO.getCurrentPage(),enterpricesDTO.getPageSize());
        List<EnterpriseVo> enterpriseList = enterpriseService.selectEnterpriseAll(enterpricesDTO);
        PageInfo<EnterpriseVo> enterpriseVoPageInfo = new PageInfo<>(enterpriseList);
        return Result.ok().data("contractList", enterpriseList ).data("total",enterpriseVoPageInfo.getTotal());
    }
    @PostMapping("queryAllStaffsAndEnterprise")
    public Result queryAllStaffsAndEnterprise(@RequestBody EnterpricesDTO enterpricesDTO){
        PageHelper.startPage(enterpricesDTO.getCurrentPage(),enterpricesDTO.getPageSize());
        List<EnterpriseVo> enterpriseList = enterpriseService.selectEnterpriseAndStaffsAll(enterpricesDTO);
        PageInfo<EnterpriseVo> enterpriseVoPageInfo = new PageInfo<>(enterpriseList);
        return Result.ok().data("contractList", enterpriseList).data("total",enterpriseVoPageInfo.getTotal());
    }

    /**
     * 根据id查询企业表信息
     *
     * @param id 主键id
     * @return com.woniu.smart.util.Result
     * @author meibing
     * @date 2022/10/20 21:40
     **/
    @PostMapping("{id}")
    public Result queryByEnterpriseId(@PathVariable Long id) {
        Enterprise enterprise = enterpriseService.selectByPrimaryKey(id);
        return Result.ok().data("enterprise", enterprise);
    }


    /**
     * 新增租户企业表
     *
     * @param contract
     * @return com.woniu.smart.util.Result
     * @author meibing
     * @date
     **/
    @PostMapping("")
    public Result insertEnterprise(@RequestBody Enterprise contract) {
        int i = enterpriseService.insertSelective(contract);
        if (i > 0) {
            return Result.ok().data("return", i);
        } else {
            return Result.error("新增租户企业表失败");
        }
    }


    @PostMapping("selByNameEnterprise")
    public Result selByNameEnterprise(@RequestBody EnterpricesDTO enterpricesDTO){
        SelByNameEnterpriseVo enterpriseVo = enterpriseService.selByNameEnterprise(enterpricesDTO);
        return Result.ok().data("enterpriseVo",enterpriseVo);
    }


    /**
     * 新增租户企业表
     *
     * @param contract
     * @return com.woniu.smart.util.Result
     * @author meibing
     * @date
     **/
    @PostMapping("inserts")
    public Result insertSelectives(@RequestBody EnterpricesDTO contract) {
        int i = enterpriseService.insertSelectives(contract);
        if (i > 0) {
            return Result.ok().data("return", i);
        } else {
            return Result.error("新增租户企业表失败");
        }
    }

    /**
     * @param id
     * @Description 删除单个
     * @author meibing
     * @date 14:41:24
     * @return: com.woniu.smart.util.Result
     */
    @DeleteMapping("{id}")
    public Result deleteEnterprisebyid(@PathVariable("id") Long id) {
        int i = enterpriseService.deleteByPrimaryKey(id);
        if (i > 0) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    /**
     * @param eids
     * @Description 批量删除
     * @author meibing
     * @date 14:41:09
     * @return: com.woniu.smart.util.Result
     */

    @DeleteMapping("deleteAll")
    public Result deleteEnterpriseAll(@RequestParam("eids") List<Integer> eids) {
        int i = enterpriseService.deleteAllByList(eids);
        if (i > 0) {
            return Result.ok();
        } else {
            return Result.error();
        }
        /**
         * @Description 公司分组查自己员工数据
         * @Param [java.util.List<java.lang.Integer>]
         * @Return com.woniu.smart.util.Result
         * @Author WangMr
         * @Date Create by 2022/10/31 19:49
         */
    }@GetMapping("group")
    public Result selectGroupByEnterprise() {
        List<EChartsEnterpriseVO> list = enterpriseService.selectGroupEnterprise();
        List<EChartsEnterpriseVO> list2 = enterpriseService.selectGroupEnterprise2();
        return Result.ok().data("enterprises",list).data("enterprises2",list2);
    }

}
